 <template>
   <Card :shadow="shadow">
      <div class="card-name">
        <div class="name width2">收款账户管理</div>
      </div>
      <div class="content-con">
        <div class="bank-add">
          <i-button size="large" @click="addPayee" class="btn-green">新增收款账号</i-button>
        </div>
        <div class="bank-form">
          <div class="form-item">
            <i-table :columns="foodColumns" :data="foodData"></i-table> <!-- table组件 -->
          </div>
          <div class="page">
            <Page :id="pageId"
             :total="dataCount"
             :page-size="pageSize"
             style="float: left;"
             @on-change="changepage"
             @on-page-size-change="nowPageSize"
             show-total show-sizer show-elevator></Page> <!-- page组件 -->
            <Button style="float: left;margin-left: 10px;" type="primary" @click="goElevatorPage">确认</Button>
          </div>
        </div>
      </div>
   </Card>

 </template>
<script>

let testData = {
  histories: [
    {
      payee: '测试001',
      bankNumber: '111',
      bankName: '浦发银行',
      payeeAmount: '30000',
      status: '开启'
    },
    {
      payee: '测试002',
      bankNumber: '321212334333331111',
      bankName: '浦发银行',
      payeeAmount: '1000000',
      status: '关闭'
    },
    {
      payee: '测试001',
      bankNumber: '111',
      bankName: '浦发银行',
      payeeAmount: '30000',
      status: '开启'
    }
  ]
}
export default {
  name: 'account_payee',
  props: {
    shadow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      value: '',
      pageId: 'targetpage',
      ajaxHistoryData: [],
      exportdata: [],
      // 初始化信息总条数
      dataCount: 0,
      // 每页显示多少条
      pageSize: 10,
      pageCurrent: 1, // 当前页
      foodColumns: [
        {
          type: 'selection',
          width: 60,
          align: 'center',
          fixed: 'left'
        },
        {
          title: '收款账户人',
          key: 'payee'
        },
        {
          title: '收款账号',
          key: 'bankNumber'
        },
        {
          title: '开户行',
          key: 'bankName'
        },
        {
          title: '总收款金额',
          key: 'payeeAmount'
        },
        {
          title: '状态',
          key: 'status',
		  render: (h, params) => {
		    const statusName = params.row.status == '0' ? '开启' : '关闭'
		    if (statusName == '开启') {
		      return h('span', {
		        style: {
		          color: '#4daa5d'
		        }
		      }, statusName)
		    } else if (statusName == '关闭') {
		      return h('span', {
		        style: {
		          color: '#515A6E'
		        }
		      }, statusName)
		    }
		  }
        },
        {
          title: '操作',
          key: 'action',
          width: 80,
          align: 'center',
          // render (row, column, index) {
          //     return `<i-button type="primary" size="small" @click="show(${index})">查看</i-button>`;
          // }
          render: (h, params) => {
            return h('div', [
              h(
                'Button', {
                  props: {
                    size: 'small'
                  },
                  style: {
                    color: 'white',
                    padding: '0 5px',
                    height: '24px',
                    fontSize: '12px',
                    backgroundColor: '#009688'
                  },
                  on: {
                    click: () => {
                      this.$router.push({
                        name: 'payee-edit'
                      })
                      // this.show(params.index)
                      // e.stopPropagation(); //阻止冒泡
                      // alert('查看');
                    }
                  }
                },
                '编辑'
              )
            ])
          }
        }
      ],
      foodData: []
    }
  },
  methods: {
    // 获取历史记录信息
    handleListApproveHistory () {
      // 保存取到的所有数据
      this.ajaxHistoryData = testData.histories
      this.exportdata = testData.histories
      this.dataCount = testData.histories.length
      // 初始化显示，小于每页显示条数，全显，大于每页显示条数，取前每页条数显示
      if (testData.histories.length < this.pageSize) {
        this.foodData = this.ajaxHistoryData
      } else {
        this.foodData = this.ajaxHistoryData.slice(0, this.pageSize)
      }
    },
    // 点击切换页面
    changepage (index) {
      // 需要显示开始数据的index,(因为数据是从0开始的，页码是从1开始的，需要-1)
      let _start = (index - 1) * this.pageSize
      // 需要显示结束数据的index
      let _end = index * this.pageSize
      // 截取需要显示的数据
      this.foodData = this.ajaxHistoryData.slice(_start, _end)
      // 储存当前页
      this.pageCurrent = index
    },
    // 每页显示的数据条数
    nowPageSize (index) {
      let _start = (this.pageCurrent - 1) * index
      let _end = this.pageCurrent * index
      this.foodData = this.ajaxHistoryData.slice(_start, _end)
      // 实时获取当前需要显示的条数
      this.pageSize = index
    },

    // 点击确认，跳转到输入的页码
    goElevatorPage () {
      var evtObj
      var thisPage = document.getElementById(this.pageId)
      // var thisPage=this.$refs.myPage;//也可以用ref来获取page这个dom
      var elevatorDiv = thisPage.getElementsByClassName('ivu-page-options-elevator')
      // 如果thisPage这里报错了,请核对上文的获取page的DOM的语句是否获取不到
      if (elevatorDiv && elevatorDiv.length > 0) {
        var pageInput = elevatorDiv[0].getElementsByTagName('input')[0]
        if (window.KeyEvent) { // firefox 浏览器下模拟事件
          evtObj = document.createEvent('KeyEvents')
          evtObj.initKeyEvent('keyup', true, true, window, true, false, false, false, 13, 0)
        } else { // chrome 浏览器下模拟事件
          evtObj = document.createEvent('UIEvents')
          evtObj.initUIEvent('keyup', true, true, window, 1)
          delete evtObj.keyCode
          if (typeof evtObj.keyCode === 'undefined') { // 为了模拟keycode
            Object.defineProperty(evtObj, 'keyCode', { value: 13 })
          } else {
            evtObj.key = String.fromCharCode(13)
          }
        }
        pageInput.dispatchEvent(evtObj)
      }
    },
    // 新增收款账户
    addPayee () {
      this.$router.push({ name: 'payee-add' })
    }
  },
  created () {
    this.handleListApproveHistory()
  }
}
</script>
 <style lang="less">
 </style>
